// pages/my/shop/index/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShowNamePop: false,//设置名称弹窗
        isShowCatePop: false,//设置品类弹窗
        isShowImgPop: false,//设置头像弹窗
        isShowBgImgPop: false,//设置照片弹窗
        isShowNoticePop: false,//设置公告弹窗
        isShowAddressPop: false,//设置地址弹窗
        isShowUnitPricePop: false,//是否显示人均单价弹窗
        shopName: '',//店铺名称
        category: [],//经营品类
        image: '',//店铺头像
        shop_images: [],//店铺图片
        notice: '',//门店公告
        address: '',//店铺地址
        phoneArr: [13233334444,13222223333],
        rate: 0,//费率
        grade: '',//店铺评分
        person_consume: 0,//人均单价

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    //获取详情
    getInfo: function() {
        wx.$http({
            load: true,
            url: '/business/shop-info',
            method: 'get',
        }).then((res) => {
            if(res.code == 1){
                let data = res.data;
                this.setData({
                    shopName: data.name,//店铺名称
                    category: data.category,//经营品类
                    image: data.avatar,//店铺头像
                    notice: data.notice,//门店公告
                    address: data.address_name+data.address_info,//店铺地址
                    phoneArr: data.shop_phone,//店铺电话
                    shop_images: data.shop_images,//店铺照片
                    person_consume: data.person_consume,//人均单价
                    rate: data.rate
                })
            }
        })
    },
    //打开弹窗
    openPop: function (e) {
        let name= e.currentTarget.dataset.name;
        this.setData({
            [name]: true
        })
    },
    //设置门店名称
    saveName: function(e) {
        let obj = e.detail;
        let {shopName} = this.data;
        this.setData({
            isShowNamePop: false
        })
        if(obj && obj.change && obj.text != shopName){
            this.setShopInfo({name: obj.text})
        }
    },
    //保存经营品类
    saveCate: function(e) {
        let obj = e.detail;
        let {category} = this.data;
        this.setData({
            isShowCatePop: false
        })
        if(obj && obj.change){
            this.setShopInfo({category: JSON.stringify(obj.selected)})
        }
    },
    //保存头像
    saveImg: function (e) {
        let obj = e.detail;
        this.setData({
            isShowImgPop: false
        })
        if(obj && obj.change){
            this.setShopInfo({avatar: obj.imgs[0]})
        }
    },
    //保存公告
    saveNotice: function (e) {
        let obj = e.detail;
        let {notice} = this.data;
        this.setData({
            isShowNoticePop: false
        })
        if(obj && obj.change && obj.text != notice){
            if(obj && obj.change){
                this.setShopInfo({notice: obj.text})
            }
        }
    },
    //保存地址
    saveAddress: function(e) {
        let obj = e.detail;
        this.setData({
            isShowAddressPop: false
        })
        if(obj && obj.change ){
            this.setShopInfo({
                address_name: obj.addressObj.name,
                address: obj.addressObj.name,
                address_info: obj.addressObj.info || '',
                lat: obj.addressObj.lat,
                lon: obj.addressObj.lon
            })
        }
    },
    //保存店铺图片
    saveBgImg: function(e) {
        let obj = e.detail;
        this.setData({
            isShowBgImgPop: false
        })
        if(obj && obj.change ){
            this.setShopInfo({
                shop_images: JSON.stringify(obj.imgs)
            })
        }
    },
    //保存人均单价
    saveUnitPrice: function(e) {
        let obj = e.detail;
        this.setData({
            isShowUnitPricePop: false
        })
        if(obj && obj.change ){
            this.setShopInfo({
                person_consume: obj.value
            })
        }
    },









    //设置内容
    setShopInfo: function (obj) {
        wx.$http({
            load: true,
            url: `/business/shop-edit`,
            data: obj
        }).then((res) => {
            if(res.code == 1){
                this.getInfo();
                wx.showToast({
                    title: '操作成功',
                })
            }
        })
    }
})